
/**
  ******************************************************************************
  * Copyright 2021 The Microbee Authors. All Rights Reserved.
  * 
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
  * 
  * http://www.apache.org/licenses/LICENSE-2.0
  * 
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  * 
  * @file       fms_param.c
  * @author     baiyang
  * @date       2021-8-23
  ******************************************************************************
  */

/*----------------------------------include-----------------------------------*/
#include "fms.h"
/*-----------------------------------macro------------------------------------*/

/*----------------------------------typedef-----------------------------------*/

/*---------------------------------prototype----------------------------------*/

/*----------------------------------variable----------------------------------*/
// 0:ANGLE_MODE, 1: ANGLE_RATE_MODE, 2: ALT_HOLD_MODE, 3: AUTO_MODE, 5:LOITER_MODE
static param_t var_info[] = {
    PARAM_DEFINE_UINT8(FLTMODE_CH,5),
    PARAM_DEFINE_UINT8(FLTMODE1,0),
    PARAM_DEFINE_UINT8(FLTMODE2,0),
    PARAM_DEFINE_UINT8(FLTMODE3,0),
    PARAM_DEFINE_UINT8(FLTMODE4,0),
    PARAM_DEFINE_UINT8(FLTMODE5,0),
    PARAM_DEFINE_UINT8(FLTMODE6,0),
};
PARAM_GROUP_DEFINE(FLIGHTMODE, var_info);

static param_t var_info2[] = { 
    PARAM_DEFINE_INT32(RTL_ALT,   1500),
    PARAM_DEFINE_FLOAT(RTL_CONE_SLOPE,     3.0f),
    PARAM_DEFINE_INT16(RTL_SPEED,   0),
    PARAM_DEFINE_INT16(RTL_ALT_FINAL, 0),
    PARAM_DEFINE_INT16(RTL_CLIMB_MIN,  0),
    PARAM_DEFINE_INT32(RTL_LOIT_TIME, 5000),
    PARAM_DEFINE_INT8(RTL_ALT_TYPE, 0),
    PARAM_DEFINE_INT32(RTL_OPTIONS, 0),
};
PARAM_GROUP_DEFINE(RTL, var_info2);

static param_t var_info3[] = {
    PARAM_DEFINE_INT16(FORMAT_VERSION, 0),

    PARAM_DEFINE_INT16(ANGLE_MAX,4500),

    // @Description: Controls major frame class for multicopter component
    // @Values: 0:Undefined, 1:Quad, 2:Hexa, 3:Octa, 4:OctaQuad, 5:Y6, 6:Heli, 7:Tri, 8:SingleCopter, 9:CoaxCopter, 10:BiCopter, 11:Heli_Dual, 12:DodecaHexa, 13:HeliQuad, 14:Deca
    PARAM_DEFINE_INT8(FRAME_CLASS,0),

    // @Description: Controls motor mixing for multicopters.  Not used for Tri or Traditional Helicopters.
    // @Values: 0:Plus, 1:X, 2:V, 3:H, 4:V-Tail, 5:A-Tail, 10:Y6B, 11:Y6F, 12:BetaFlightX, 13:DJIX, 14:ClockwiseX, 15: I, 18: BetaFlightXReversed
    PARAM_DEFINE_INT8(FRAME_TYPE,1),
    PARAM_DEFINE_INT8(FS_THR_ENABLE, 1),
    PARAM_DEFINE_INT16(FS_THR_VALUE, 975),
    PARAM_DEFINE_INT16(THR_DZ, 100),

    PARAM_DEFINE_FLOAT(PILOT_THR_FILT,0.0f),
    PARAM_DEFINE_FLOAT(PILOT_Y_RATE,PILOT_Y_RATE_DEFAULT),
    PARAM_DEFINE_FLOAT(ACRO_Y_EXPO,ACRO_Y_EXPO_DEFAULT),

    // @Param: PILOT_THR_BHV
    // @DisplayName: Throttle stick behavior
    // @Description: Bitmask containing various throttle stick options. TX with sprung throttle can set PILOT_THR_BHV to "1" so motor feedback when landed starts from mid-stick instead of bottom of stick.
    // @User: Standard
    // @Values: 0:None,1:Feedback from mid stick,2:High throttle cancels landing,4:Disarm on land detection
    // @Bitmask: 0:Feedback from mid stick,1:High throttle cancels landing,2:Disarm on land detection
    PARAM_DEFINE_INT16(PILOT_THR_BHV,0),
    PARAM_DEFINE_INT8(DISARM_DELAY,10),

    // 单位：cm/s
    PARAM_DEFINE_INT16(PILOT_SPEED_UP,250),
    PARAM_DEFINE_INT16(PILOT_SPEED_DN,0),
    PARAM_DEFINE_INT16(PILOT_ACCEL_Z,250),
    PARAM_DEFINE_FLOAT(PILOT_TKOFF_ALT, 0.0f),
    PARAM_DEFINE_INT8(AHRS_ORIENTATION, 0),

    PARAM_DEFINE_INT8(ESC_CALIBRATION, 0),
    PARAM_DEFINE_INT16(GPS_HDOP_GOOD, 140),

    PARAM_DEFINE_INT8(WP_YAW_BEHAVIOR, 2),
    PARAM_DEFINE_INT16(LAND_ALT_LOW, 1000),

    PARAM_DEFINE_INT16(LAND_SPEED, 50),
    PARAM_DEFINE_INT16(LAND_SPEED_HIGH, 0),
    PARAM_DEFINE_INT8(LAND_REPOSITION, 1),
    PARAM_DEFINE_FLOAT(WP_NAVALT_MIN, 0),
    PARAM_DEFINE_INT32(FS_OPTIONS, (1<<4)),
    PARAM_DEFINE_INT8(FS_GCS_ENABLE, 0),
    PARAM_DEFINE_FLOAT(FS_GCS_TIMEOUT, 5),

    PARAM_DEFINE_INT32(GUID_OPTIONS, 0),
    PARAM_DEFINE_FLOAT(GUID_TIMEOUT, 3.0f),

    // @Param: GND_EFFECT_COMP
    // @DisplayName: Ground Effect Compensation Enable/Disable
    // @Description: Ground Effect Compensation Enable/Disable
    // @Values: 0:Disabled,1:Enabled
    // @User: Advanced
    PARAM_DEFINE_INT8(GND_EFFECT_COMP, 1),

    // @Param: TKOFF_SLEW_TIME
    // @DisplayName: Slew time of throttle during take-off
    // @Description: Time to slew the throttle from minimum to maximum while checking for a succsessful takeoff.
    // @Units: s
    // @Range: 0.25 5.0
    // @User: Standard
    PARAM_DEFINE_FLOAT(TKOFF_SLEW_TIME, 2.0f),

    // @Param: AUTO_OPTIONS
    // @DisplayName: Auto mode options
    // @Description: A range of options that can be applied to change auto mode behaviour. Allow Arming allows the copter to be armed in Auto. Allow Takeoff Without Raising Throttle allows takeoff without the pilot having to raise the throttle. Ignore pilot yaw overrides the pilot's yaw stick being used while in auto.
    // @Bitmask: 0:Allow Arming,1:Allow Takeoff Without Raising Throttle,2:Ignore pilot yaw
    // @User: Advanced
    PARAM_DEFINE_INT32(AUTO_OPTIONS, 0),

    // @Param: FS_VIBE_ENABLE
    // @DisplayName: Vibration Failsafe enable
    // @Description: This enables the vibration failsafe which will use modified altitude estimation and control during high vibrations
    // @Values: 0:Disabled, 1:Enabled
    // @User: Standard
    PARAM_DEFINE_INT8(FS_VIBE_ENABLE, 1),

    // @Param: RC_SPEED
    // @DisplayName: ESC Update Speed
    // @Description: This is the speed in Hertz that your ESCs will receive updates
    // @Units: Hz
    // @Range: 50 490
    // @Increment: 1
    // @User: Advanced
    PARAM_DEFINE_INT16(RC_SPEED, RC_FAST_SPEED),

    // @Param: FS_CRASH_CHECK
    // @DisplayName: Crash check enable
    // @Description: This enables automatic crash checking. When enabled the motors will disarm if a crash is detected.
    // @Values: 0:Disabled, 1:Enabled
    // @User: Advanced
    PARAM_DEFINE_INT8(FS_CRASH_CHECK,    1),

    // @Param: FLIGHT_OPTIONS
    // @DisplayName: Flight mode options
    // @Description: Flight mode specific options
    // @Bitmask: 0:Disable thrust loss check, 1:Disable yaw imbalance warning, 2:Release gripper on thrust loss
    // @User: Advanced
    PARAM_DEFINE_UINT32(FLIGHT_OPTIONS, 0),

#if RANGEFINDER_ENABLED == ENABLED
    // @Param: RNGFND_FILT
    // @DisplayName: Rangefinder filter
    // @Description: Rangefinder filter to smooth distance.  Set to zero to disable filtering
    // @Units: Hz
    // @Range: 0 5
    // @Increment: 0.05
    // @User: Standard
    // @RebootRequired: True
    PARAM_DEFINE_FLOAT(RNGFND_FILT, RANGEFINDER_FILT_DEFAULT),
#endif

    // @Param: SURFTRAK_MODE
    // @DisplayName: Surface Tracking Mode
    // @Description: set which surface to track in surface tracking
    // @Values: 0:Do not track, 1:Ground, 2:Ceiling
    // @User: Advanced
    PARAM_DEFINE_INT8(SURFTRAK_MODE, SURFACE_TYPE_GROUND),

    // @Param: SURFTRAK_TC
    // @DisplayName: Surface Tracking Filter Time Constant
    // @Description: Time to achieve 63.2% of the surface altitude measurement change.  If 0 filtering is disabled
    // @Units: s
    // @Range: 0 5
    // @User: Advanced
    PARAM_DEFINE_FLOAT(SURFTRAK_TC, 1.0f),

    // @Param: LOG_BITMASK
    // @DisplayName: Log bitmask
    // @Description: Bitmap of what on-board log types to enable. This value is made up of the sum of each of the log types you want to be saved. It is usually best just to enable all basiclog types by setting this to 65535. 
    // @Bitmask: 0:Fast Attitude,1:Medium Attitude,2:GPS,3:System Performance,4:Control Tuning,5:Navigation Tuning,6:RC input,7:IMU,8:Mission Commands,9:Battery Monitor,10:RC output,11:Optical Flow,12:PID,13:Compass,15:Camera,17:Motors,18:Fast IMU,19:Raw IMU,20:Video Stabilization,21:Fast harmonic notch logging
    // @User: Standard
    PARAM_DEFINE_INT32(LOG_BITMASK,       DEFAULT_LOG_BITMASK),

#if MODE_THROW_ENABLED == ENABLED
    // @Param: THROW_NEXTMODE
    // @DisplayName: Throw mode's follow up mode
    // @Description: Vehicle will switch to this mode after the throw is successfully completed.  Default is to stay in throw mode (18)
    // @Values: 3:Auto,4:Guided,5:LOITER,6:RTL,9:Land,17:Brake,18:Throw
    // @User: Standard
    PARAM_DEFINE_INT8(THROW_NEXTMODE, 18),

    // @Param: THROW_TYPE
    // @DisplayName: Type of Type
    // @Description: Used by Throw mode. Specifies whether Copter is thrown upward or dropped.
    // @Values: 0:Upward Throw,1:Drop
    // @User: Standard
    PARAM_DEFINE_INT8(THROW_TYPE, Throw_Upward),

    // @Param: THROW_MOT_START
    // @DisplayName: Start motors before throwing is detected
    // @Description: Used by Throw mode. Controls whether motors will run at the speed set by MOT_SPIN_MIN or will be stopped when armed and waiting for the throw.
    // @Values: 0:Stopped,1:Running
    // @User: Standard
    PARAM_DEFINE_INT8(THROW_MOT_START, (float)Throw_PMS_STOPPED),
#endif
};
PARAM_GROUP_DEFINE(VEHICLE, var_info3);

/*-------------------------------------os-------------------------------------*/

/*----------------------------------function----------------------------------*/
/**
  * @brief       
  * @param[in]     
  * @param[out]  
  * @retval      
  * @note        
  */
void fms_assign_param()
{
    param_link_variable(PARAM_ID(FLIGHTMODE, FLTMODE1), &fms.flight_modes[0]);
    param_link_variable(PARAM_ID(FLIGHTMODE, FLTMODE2), &fms.flight_modes[1]);
    param_link_variable(PARAM_ID(FLIGHTMODE, FLTMODE3), &fms.flight_modes[2]);
    param_link_variable(PARAM_ID(FLIGHTMODE, FLTMODE4), &fms.flight_modes[3]);
    param_link_variable(PARAM_ID(FLIGHTMODE, FLTMODE5), &fms.flight_modes[4]);
    param_link_variable(PARAM_ID(FLIGHTMODE, FLTMODE6), &fms.flight_modes[5]);
    param_link_variable(PARAM_ID(FLIGHTMODE, FLTMODE_CH), &fms.flight_mode_chan);

    param_link_variable(PARAM_ID(VEHICLE, FORMAT_VERSION), &fms.g.format_version);

    param_link_variable(PARAM_ID(VEHICLE,FRAME_CLASS), &fms.frame_class);
    param_link_variable(PARAM_ID(VEHICLE,FRAME_TYPE), &fms.frame_type);

    param_link_variable(PARAM_ID(VEHICLE,FS_THR_ENABLE), &fms.g.failsafe_throttle);
    param_link_variable(PARAM_ID(VEHICLE,FS_THR_VALUE), &fms.g.failsafe_throttle_value);
    param_link_variable(PARAM_ID(VEHICLE,THR_DZ), &fms.g.throttle_deadzone);

    param_link_variable(PARAM_ID(VEHICLE, PILOT_SPEED_UP), &fms.g.pilot_speed_up);
    param_link_variable(PARAM_ID(VEHICLE, PILOT_SPEED_DN), &fms.g.pilot_speed_dn);

    param_link_variable(PARAM_ID(VEHICLE,PILOT_Y_RATE), &fms.g.pilot_y_rate);
    param_link_variable(PARAM_ID(VEHICLE,ACRO_Y_EXPO), &fms.g.pilot_y_expo);

    param_link_variable(PARAM_ID(VEHICLE,PILOT_ACCEL_Z), &fms.g.pilot_accel_z);
    param_link_variable(PARAM_ID(VEHICLE,DISARM_DELAY), &fms.g.disarm_delay);

    param_link_variable(PARAM_ID(VEHICLE,PILOT_THR_BHV), &fms.g.throttle_behavior);
    param_link_variable(PARAM_ID(VEHICLE,PILOT_THR_FILT), &fms.g.throttle_filt);

    param_link_variable(PARAM_ID(VEHICLE,ANGLE_MAX), &fms.g.angle_max);
    param_link_variable(PARAM_ID(VEHICLE,PILOT_TKOFF_ALT), &fms.g.pilot_takeoff_alt);

    param_link_variable(PARAM_ID(VEHICLE,GPS_HDOP_GOOD), &fms.g.gps_hdop_good);

    param_link_variable(PARAM_ID(VEHICLE,WP_YAW_BEHAVIOR), &fms.g.wp_yaw_behavior);
    param_link_variable(PARAM_ID(VEHICLE,LAND_ALT_LOW), &fms.g.land_alt_low);

    param_link_variable(PARAM_ID(VEHICLE,LAND_SPEED), &fms.g.land_speed);
    param_link_variable(PARAM_ID(VEHICLE,LAND_SPEED_HIGH), &fms.g.land_speed_high);

    param_link_variable(PARAM_ID(VEHICLE,LAND_REPOSITION), &fms.g.land_repositioning);
    param_link_variable(PARAM_ID(VEHICLE,WP_NAVALT_MIN), &fms.g.wp_navalt_min);

    param_link_variable(PARAM_ID(RTL,RTL_ALT), &fms.g.rtl_altitude);
    param_link_variable(PARAM_ID(RTL,RTL_CONE_SLOPE), &fms.g.rtl_cone_slope);
    param_link_variable(PARAM_ID(RTL,RTL_SPEED), &fms.g.rtl_speed_cms);
    param_link_variable(PARAM_ID(RTL,RTL_ALT_FINAL), &fms.g.rtl_alt_final);
    param_link_variable(PARAM_ID(RTL,RTL_CLIMB_MIN), &fms.g.rtl_climb_min);
    param_link_variable(PARAM_ID(RTL,RTL_LOIT_TIME), &fms.g.rtl_loiter_time);
    param_link_variable(PARAM_ID(RTL,RTL_ALT_TYPE), &fms.g.rtl_alt_type);
    param_link_variable(PARAM_ID(RTL,RTL_OPTIONS), &fms.g.rtl_options);

    param_link_variable(PARAM_ID(VEHICLE,FS_VIBE_ENABLE), &fms.g.fs_vibe_enabled);

    param_link_variable(PARAM_ID(VEHICLE,FS_OPTIONS), &fms.g.fs_options);
    param_link_variable(PARAM_ID(VEHICLE,FS_GCS_ENABLE), &fms.g.failsafe_gcs);
    param_link_variable(PARAM_ID(VEHICLE,FS_GCS_TIMEOUT), &fms.g.fs_gcs_timeout);

    param_link_variable(PARAM_ID(VEHICLE,GUID_OPTIONS), &fms.g.guided_options);
    param_link_variable(PARAM_ID(VEHICLE,GUID_TIMEOUT), &fms.g.guided_timeout);

    param_link_variable(PARAM_ID(VEHICLE,GND_EFFECT_COMP), &fms.g.gndeffect_comp_enabled);
    param_link_variable(PARAM_ID(VEHICLE,TKOFF_SLEW_TIME), &fms.g.takeoff_throttle_slew_time);
    param_link_variable(PARAM_ID(VEHICLE,AUTO_OPTIONS), &fms.g.auto_options);

    param_link_variable(PARAM_ID(VEHICLE,RC_SPEED), &fms.g.rc_speed);
    param_link_variable(PARAM_ID(VEHICLE,FS_CRASH_CHECK), &fms.g.fs_crash_check);
    param_link_variable(PARAM_ID(VEHICLE,FLIGHT_OPTIONS), &fms.g.flight_options);

#if RANGEFINDER_ENABLED == ENABLED
    param_link_variable(PARAM_ID(VEHICLE,RNGFND_FILT), &fms.g.rangefinder_filt);
#endif

    param_link_variable(PARAM_ID(VEHICLE,SURFTRAK_MODE), &fms.g.surftrak_mode);
    param_link_variable(PARAM_ID(VEHICLE,SURFTRAK_TC), &fms.g.surftrak_tc);

#if MODE_THROW_ENABLED == ENABLED
    param_link_variable(PARAM_ID(VEHICLE, THROW_NEXTMODE), &fms.g.throw_nextmode);
    param_link_variable(PARAM_ID(VEHICLE, THROW_TYPE), &fms.g.throw_type);
    param_link_variable(PARAM_ID(VEHICLE, THROW_MOT_START), &fms.g.throw_motor_start);
#endif
}

/*------------------------------------test------------------------------------*/


